//
//  passwordViewController.m
//  随便点
//
//  Created by MWJ on 15/3/12.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "passwordViewController.h"
#import "AppDelegate.h"
#define zifu 15
#define KeditPwd @"http://www.51anys.com:8080/waimaiwang/user/editPwd"
@interface passwordViewController (){
    UITextField *dqmm;
    UITextField *xdmm;
    UITextField *zcsr;
    NSString *str;
    AppDelegate *app;
    NSString *fanhui;
}

@end

@implementation passwordViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"zixingche"]]];
        self.navigationItem.title=@"修改密码";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    str = app._id;
    [self loading];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loading{
    [self text1];
    [self text2];
    [self text3];
    [self label1];
    [self label2];
    [self label3];
    [self button1];
    [self button2];
}
-(void)text1{
    UITextField *dangqian = [[UITextField alloc] init];
    CGFloat dangqianX =100;
    CGFloat dangqianY =100;
    CGFloat dangqianW =[UIScreen mainScreen].bounds.size.width-130;
    CGFloat dangqianH =zifu*2;
    dangqian.borderStyle = UITextBorderStyleRoundedRect;
    dangqian.secureTextEntry = YES;
    [dangqian setBackgroundColor:[UIColor whiteColor]];
    dangqian.font = [UIFont systemFontOfSize:15];
    dangqian.frame = CGRectMake(dangqianX, dangqianY, dangqianW, dangqianH);
    [self.view addSubview:dangqian];
    dqmm = dangqian;
}
-(void)text2{
    UITextField *new = [[UITextField alloc]init];
    CGFloat newX = 100;
    CGFloat newY = 150;
    CGFloat newW = [UIScreen mainScreen].bounds.size.width-130;
    CGFloat newH = zifu*2;
    new.borderStyle = UITextBorderStyleRoundedRect;
    new.secureTextEntry = YES;
    [new setBackgroundColor:[UIColor whiteColor]];
    new.font = [UIFont systemFontOfSize:15];
    new.frame = CGRectMake(newX, newY, newW, newH);
    [self.view addSubview:new];
    xdmm = new;
}
-(void)text3{
    UITextField *zaici = [[UITextField alloc]init];
    CGFloat zaiciX = 100;
    CGFloat zaiciY = 200;
    CGFloat zaiciW = [UIScreen mainScreen].bounds.size.width-130;
    CGFloat zaiciH = zifu*2;
    zaici.borderStyle = UITextBorderStyleRoundedRect;
    zaici.secureTextEntry = YES;
    [zaici setBackgroundColor:[UIColor whiteColor]];
    zaici.font = [UIFont systemFontOfSize:15];
    zaici.frame = CGRectMake(zaiciX, zaiciY, zaiciW, zaiciH);
    [self.view addSubview:zaici];
    zcsr = zaici;
}
-(void)label1{
    UILabel *lab1 = [[UILabel alloc]init];
    CGFloat lab1X = 30;
    CGFloat lab1Y = 100;
    CGFloat lab1W = zifu*4;
    CGFloat lab1H = zifu*2;
    lab1.text = @"当前密码";
    lab1.textColor = [UIColor blackColor];
    lab1.font = [UIFont systemFontOfSize:15];
    lab1.frame = CGRectMake(lab1X, lab1Y, lab1W, lab1H);
    [self.view addSubview:lab1];
}
-(void)label2{
    UILabel *lab2 = [[UILabel alloc]init];
    CGFloat lab2X = 30;
    CGFloat lab2Y = 150;
    CGFloat lab2W = zifu*4;
    CGFloat lab2H = zifu*2;
    lab2.text = @"新的密码";
    lab2.textColor = [UIColor blackColor];
    lab2.font = [UIFont systemFontOfSize:15];
    lab2.frame = CGRectMake(lab2X, lab2Y, lab2W, lab2H);
    [self.view addSubview:lab2];
}
-(void)label3{
    UILabel *lab3 = [[UILabel alloc]init];
    CGFloat lab3X = 30;
    CGFloat lab3Y = 200;
    CGFloat lab3W = zifu*4;
    CGFloat lab3H = zifu*2;
    lab3.text = @"再次输入";
    lab3.textColor = [UIColor blackColor];
    lab3.font = [UIFont systemFontOfSize:15];
    lab3.frame = CGRectMake(lab3X, lab3Y, lab3W, lab3H);
    [self.view addSubview:lab3];
}
-(void)button1{
    UIButton *xiugai = [[UIButton alloc]init];
    CGFloat xiugaiX = 60;
    CGFloat xiugaiY = 300;
    CGFloat xiugaiW = 90;
    CGFloat xiugaiH = zifu*2;
    xiugai.titleLabel.font = [UIFont systemFontOfSize:20];
    xiugai.backgroundColor = [UIColor greenColor];
    xiugai.frame = CGRectMake(xiugaiX, xiugaiY, xiugaiW, xiugaiH);
    [xiugai setTitle:[NSString stringWithFormat:@"确定"] forState:UIControlStateNormal];
    [xiugai setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [xiugai setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [xiugai addTarget:self action:@selector(modify) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xiugai];
    
}
-(void)modify{
    if ([dqmm.text isEqualToString:@""]||[xdmm.text isEqualToString:@""]||[zcsr.text isEqualToString:@""]||xdmm==zcsr) {
        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入正确的密码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]show ];
    }else{
        [self XG];
    }
    
    
}
-(void)XG{
    NSURL *url=[NSURL URLWithString:KeditPwd];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"userId=%@&oldPwd=%@&newPwd=%@",str,dqmm.text,xdmm.text];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        fanhui = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:fanhui waitUntilDone:YES];
        int intStr = [fanhui intValue];
        if (intStr == 1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }
        }];
}
-(void)refreshData:(NSArray *)sender{

}
-(void)button2{
    UIButton *quxiao = [[UIButton alloc]init];
    CGFloat quxiaoX = [UIScreen mainScreen].bounds.size.width-150;
    CGFloat quxiaoY = 300;
    CGFloat quxiaoW = 90;
    CGFloat quxiaoH = zifu*2;
    quxiao.titleLabel.font = [UIFont systemFontOfSize:20];
    quxiao.backgroundColor = [UIColor redColor];
    quxiao.frame = CGRectMake(quxiaoX, quxiaoY, quxiaoW, quxiaoH);
    [quxiao setTitle:[NSString stringWithFormat:@"取消"] forState:UIControlStateNormal];
    [quxiao setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [quxiao setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [quxiao addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quxiao];
    
}
-(void)cancel{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [dqmm resignFirstResponder];
    [xdmm resignFirstResponder];
    [zcsr resignFirstResponder];
}


@end
